package com.github.cyf.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.github.cyf.common.module.response.Rs;
import com.github.cyf.domain.vo.LoginVo;
import com.github.cyf.service.UserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author chenyifan
 * @create 2024-10-16 23:09
 */
@RestController
@RequestMapping("/api")
@Api(tags = "公用操作")
@ApiSupport(order = 1)
public class ApiController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("1、用户登录")
    @ApiOperationSupport(order = 1)
    public Rs login(@RequestBody @Validated LoginVo vo) {
        String token = userService.login(vo);
        return Rs.ok().setData(token).setMsg("登录成功");
    }

    @GetMapping("/getTokenInfo")
    @ApiOperation("2、获取token信息")
    @ApiOperationSupport(order = 2)
    public Rs getTokenInfo() {
        return Rs.ok().setData(StpUtil.getTokenInfo());
    }

    @GetMapping("/kickOffline")
    @ApiOperation("3、踢人下线")
    @ApiOperationSupport(order = 3)
    @SaCheckPermission("online.user.kick")
    public Rs kickOffline(@RequestParam("token") String token) {
        if (StpUtil.getTokenValue().equals(token)) {
            return Rs.error("不能踢自己");
        }
        StpUtil.kickoutByTokenValue(token);
        return Rs.ok("踢人下线成功");
    }

    @GetMapping("/logout")
    @ApiOperation("4、退出登录")
    @ApiOperationSupport(order = 4)
    public Rs logout() {
        StpUtil.logout();
        return Rs.ok("退出登录成功");
    }
}